import tkinter as tk
import json
import os
import time
from collections import OrderedDict
import threading

with open('.\\myjson\\dashboard.json', 'r', encoding='utf-8') as f:
    func_list = json.load(f, object_pairs_hook=OrderedDict)


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        # self.wm_attributes('-topmost', 1)
        self.attributes('-alpha', 0.4)
        self.bind('<Enter>', self.show)
        self.bind('<Leave>', self.hide)
        self.title('DashBoard')
        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()

        ww = 200
        wh = 450
        self.close_flag = 0
        self.geometry("%dx%d+%d+%d" % (ww, wh, (sw - ww) / 2, (sh - wh) / 2))
        self.after(1, lambda: self.focus_force())
        self.info = tk.Label(self)
        self.info.bind('<ButtonRelease-1>', self.restart_count)
        self.info.pack()
        self.flag = tk.StringVar()
        self.flag.set('无窗口')
        self.edition = tk.StringVar()
        self.edition.set('38')
        self.selection = tk.StringVar()
        self.selection.set('')

        radio_frame = tk.Frame(self)
        radio_frame.pack(pady=10)
        tk.Radiobutton(radio_frame,
                       variable=self.flag,
                       text='无窗口',
                       value='无窗口').pack(side=tk.LEFT)
        tk.Radiobutton(radio_frame,
                       variable=self.flag,
                       text='窗口模式',
                       value='窗口模式').pack(side=tk.LEFT)

        radio_frame_edition = tk.Frame(self)
        radio_frame_edition.pack(pady=10)
        tk.Radiobutton(radio_frame_edition,
                       variable=self.edition,
                       text='python3.6',
                       value='36').pack(side=tk.LEFT)
        tk.Radiobutton(radio_frame_edition,
                       variable=self.edition,
                       text='python3.8',
                       value='38').pack(side=tk.LEFT)

        radios_frame = tk.Frame(self)
        radios_frame.pack(pady=10)

        for k, v in func_list.items():

            tk.Radiobutton(radios_frame,
                           variable=self.selection,
                           text=k,
                           value=v,
                           indicatoron=False,
                           command=self.run).pack(anchor=tk.W,
                                                  fill='x',
                                                  pady=1)

    def run(self):
        self.state('icon')
        select = self.selection.get()
        flag = self.flag.get()
        edition = self.edition.get()
        if flag == '无窗口':
            os.system(
                r'start C:\Users\ys\AppData\Local\Programs\Python\Python%s-32\pythonw.exe %s' %
                (edition, select))
        else:
            os.system(
                r'start C:\Users\ys\AppData\Local\Programs\Python\Python%s-32\python.exe %s' %
                (edition, select))

    def show(self, event):
        self.attributes('-alpha', 1.0)
        self.close_flag = 0
        self.update()

    def hide(self, event):
        self.attributes('-alpha', 0.4)
        self.update()

    def auto_close(self):
        while 1:
            self.info['text'] = '%s' % (1000 - self.close_flag)
            time.sleep(5)
            self.close_flag += 1
            if self.close_flag == 1000:

                os.system(r'taskkill /F /IM pythonw.exe')
                os.system(r'taskkill /F /IM python.exe')

    def restart_count(self, event):
        self.close_flag = 0

    def start_auto_close(self):
        t = threading.Thread(target=self.auto_close)
        t.setDaemon(True)
        t.start()


if __name__ == "__main__":
    app = MainWindow()
    app.start_auto_close()
    app.mainloop()